package com.lkxicy.wos.service.impl;

import com.lkxicy.wos.mapper.SalaryPaymentMapper;
import com.lkxicy.wos.mapper.StaffMapper;
import com.lkxicy.wos.pojo.entity.SalaryPayment;
import com.lkxicy.wos.pojo.entity.SalaryReword;
import com.lkxicy.wos.mapper.SalaryRewordMapper;
import com.lkxicy.wos.pojo.entity.Staff;
import com.lkxicy.wos.service.SalaryRewordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lkxicy
 * @since 2020-12-02
 */
@Service
public class SalaryRewordServiceImpl extends ServiceImpl<SalaryRewordMapper, SalaryReword> implements SalaryRewordService {

    @Autowired
    private SalaryRewordMapper salaryRewordMapper;

    @Autowired
    private SalaryPaymentMapper salaryPaymentMapper;

    @Autowired
    private StaffMapper staffMapper;

    @Override
    public String addChangeSalaryRecord(String staffNumber, String details, double salaryChange, Date beginTime,Date endTime) {
        boolean flag=false;
        if(beginTime.equals(endTime)){
            salaryRewordMapper.changeSalary(staffNumber,details,salaryChange,beginTime);
        }
        else{
            Calendar calendar = Calendar.getInstance();
            // beginTime一直后移到大于或等于endTime
            while(true){
                if(endTime.equals(beginTime) || endTime.before(beginTime)){
                    salaryRewordMapper.changeSalary(staffNumber,details,salaryChange,beginTime);
                    return "增加工资奖惩记录成功！";
                }
                salaryRewordMapper.changeSalary(staffNumber,details,salaryChange,beginTime);
                calendar.setTime(beginTime);
                calendar.add(2,1);
                beginTime=calendar.getTime();
            }
        }
        return "增加工资奖惩记录成功！";
    }



    @Override
    public List<SalaryReword> findOneAllReword(String staffNumber) {
        return salaryRewordMapper.findOneAllReword(staffNumber);
    }

    @Override
    public String addAllChangeSalaryRecord(String details, double salaryChange, Date beginTime, Date endTime) {
        for(Staff staff:staffMapper.findAllStaff()){
            if(beginTime.equals(endTime)){
                salaryRewordMapper.changeSalary(staff.getStaffNumber(),details,salaryChange,beginTime);
            }
            else{
                Calendar calendar = Calendar.getInstance();
                // beginTime一直后移到大于或等于endTime
                while(true){
                    if(endTime.equals(beginTime) || endTime.before(beginTime)){
                        salaryRewordMapper.changeSalary(staff.getStaffNumber(),details,salaryChange,beginTime);
                        return "增加工资奖惩记录成功！";
                    }
                    salaryRewordMapper.changeSalary(staff.getStaffNumber(),details,salaryChange,beginTime);
                    calendar.setTime(beginTime);
                    calendar.add(2,1);
                    beginTime=calendar.getTime();
                }
            }
        }
        return "增加全体员工工资奖惩记录成功！";
    }
}
